// leaderPerson/pages/member_pickUp/member_pickUp.js
var common = require("../../common/common.js").common;

const app = getApp();

Page({
    /**
   * 页面的初始数据
   */
    data: {
        page: 1,
        chooseAll: 1,
        chooseNum: "",
        member_id: "",
        clerk: {}
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        var that = this;
        if (options.scene) {
            var scene = decodeURIComponent(options.scene);
            let member_id = scene.split(":");
            this.setData({
                member_id: member_id[1]
            });
        }
        that.setData({
            date: options.date || "",
            clerk: wx.getStorageSync("clerk"),
            domain: app.globalData.domain.slice(0, -10)
        });
        this.getData();
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {
        wx.stopPullDownRefresh();
        this.getData();
    },
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {},
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {},
    getData: function() {
        var that = this;
        var clerk = that.data.clerk;
        var openid = "";
        if (clerk.wom_status == 0 || clerk.wom_status == undefined) {
            openid = wx.getStorageSync("user").openid;
        } else {
            openid = clerk.leader_openid;
        }
        var date = that.data.date;
        var member_id = that.data.member_id;
        common.request({
            endpoint: "My/waitpick/mymember_waitconfirm2",
            method: "post",
            data: {
                openid: openid,
                date: date,
                user_id: member_id
            },
            success: res => {
                let data = that.data.order;
                var newData = that.orderList(res.extra);
                that.setData({
                    list: res.extra,
                    chooseNum: res.extra.length
                });
            }
        });
    },
    orderList(data) {
        if (data || data.length > 0) {
            data.forEach((item, index) => {
                var newProArry = [];
                var deliveryCost = [];
                var aftermarket_num = 0;
                //可申请售后订单数量
                                var status4_num = 0;
                //待取货订单数量
                                var delivery1_num = 0;
                //自提订单数量
                                var delivery2_num = 0;
                //团长配送订单数量
                                var delivery3_num = 0;
                //平台配送订单数量
                // 拼运费字符串
                                if (item.esb_delivery && item.esb_delivery != "0.00") {
                    deliveryCost.push("平台配送费：￥" + item.esb_delivery);
                }
                if (item.express_delivery && item.express_delivery != "0.00") {
                    deliveryCost.push("快递配送费：￥" + item.express_delivery);
                }
                if (item.leader_delivery && item.leader_delivery != "0.00") {
                    deliveryCost.push("团长配送费：￥" + item.leader_delivery);
                }
                item.item.forEach((proInfo, proIndx) => {
                    if (item.aftermarket_overtime == 1) {
                        aftermarket_num++;
                    }
                    if (item.order_status == "4") {
                        status4_num++;
                    }
                    switch (proInfo.delivery_channel) {
                      case "1":
                        delivery1_num++;
                        break;

                      case "2":
                        delivery2_num++;
                        break;

                      case "3":
                        delivery3_num++;
                        break;
                    }
                    var pro = {
                        id: proInfo.id,
                        //订单id，
                        proId: proInfo.pro_id,
                        //商品id
                        icon: proInfo.icon,
                        //商品缩略图
                        proName: proInfo.alias_name,
                        //商品名称
                        spec: proInfo.product_info,
                        //规格
                        money_refund: proInfo.money_refund,
                        //是否是满减商品
                        price: proInfo.price,
                        //商品单价
                        num: proInfo.num,
                        //数量
                        status: proInfo.status,
                        //售后状态，-1——仅退款，-2—— 退货退款，-3退货换货
                        order_status: proInfo.order_status,
                        //订单状态
                        isExpressPro: proInfo.delivery_channel == 4 ? 1 : 0,
                        //是否是快递商品，1是，2否
                        order_status: proInfo.order_status
                    };
                    newProArry.push(pro);
                });
                var newOrder = {
                    totalNum: item.allNum,
                    item: newProArry
                };
                item.goodsListData = newOrder;
                item.deliveryCost = "(" + (deliveryCost.length > 0 ? deliveryCost.join(",") : "免配送费") + ")";
                item.status4_num = status4_num;
                item.aftermarket_num = aftermarket_num;
                item.delivery1_num = delivery1_num;
                item.delivery2_num = delivery2_num;
                item.delivery3_num = delivery3_num;
                item.choose = 1;
                let brokerage = 0;
                item.item.map(v => {
                    brokerage += parseFloat(v.ileader_brokerage);
                });
                item.brokerage = parseFloat(brokerage).toFixed(2);
            });
            return data;
        }
    },
    chooseClass: function(e) {
        var trade_no = e.currentTarget.dataset.trade;
        var list = this.data.list;
        var chooseNum = 0;
        list.forEach((item, index) => {
            if (trade_no == item.trade_no) {
                item.choose = item.choose == 1 ? 0 : 1;
            }
            if (item.choose == 1) {
                chooseNum++;
            }
        });
        this.setData({
            list: list,
            chooseNum: chooseNum
        });
    },
    confirm_pickUp: function(e) {
        var result = e.currentTarget.dataset.result;
        var that = this;
        that.setData({
            payBtn: true
        });
        var clerk = that.data.clerk;
        var date = that.data.date;
        var openid = "";
        if (clerk.wom_status == 0 || clerk.wom_status == undefined) {
            openid = wx.getStorageSync("user").openid;
        } else {
            openid = clerk.leader_openid;
        }
        wx.showModal({
            title: "提示",
            content: "是否确认该团员已取货！",
            success: function(res) {
                if (res.cancel) {} else {
                    common.request({
                        endpoint: "My/waitpick/change_status2",
                        method: "post",
                        data: {
                            openid: openid,
                            order_id: result
                        },
                        header: {
                            "content-type": "application/json"
                        },
                        success: res => {
                            wx.showToast({
                                title: "取货成功",
                                icon: "success"
                            });
                            that.getData();
                        },
                        fail: res => {
                            if (res.code == 4) {
                                wx.showToast({
                                    title: res.message,
                                    icon: "none"
                                });
                            } else {
                                wx.showToast({
                                    title: "取货失败",
                                    icon: "none"
                                });
                            }
                        }
                    });
                }
            },
            complete: function(res) {
                that.setData({
                    payBtn: false
                });
            }
        });
    },
    confirm_all: function() {
        var that = this;
        var clerk = that.data.clerk;
        var openid = "";
        if (clerk.wom_status == 0 || clerk.wom_status == undefined) {
            openid = wx.getStorageSync("user").openid;
        } else {
            openid = clerk.leader_openid;
        }
        var member_id = that.data.member_id;
        var list = that.data.list;
        var item_id = [];
        list.map(item => {
            if (item.choose == 1) {
                item.item.map(subItem => {
                    item_id.push(subItem.iid);
                });
            }
        });
        wx.showModal({
            title: "提示",
            content: "是否一键确认取货！",
            success: function(res) {
                if (res.cancel) {} else {
                    common.request({
                        endpoint: "My/waitpick/pickorder_all",
                        method: "post",
                        data: {
                            user_id: member_id,
                            openid: openid,
                            item_id: item_id
                        },
                        success: res => {
                            wx.showToast({
                                title: "取货成功",
                                icon: "success"
                            });
                            that.getData();
                        },
                        fail(err) {
                            wx.showModal({
                                title: "提示",
                                content: err.message,
                                showCancel: false
                            });
                        }
                    });
                }
            }
        });
    },
    choosePickAll: function() {
        var list = this.data.list;
        var chooseNum = 0;
        if (this.data.chooseAll == 0) {
            list.forEach((item, index) => {
                item.choose = 1;
                if (item.choose == 1) {
                    chooseNum++;
                }
            });
            this.setData({
                chooseAll: 1,
                list: list,
                chooseNum: chooseNum
            });
        } else {
            list.forEach((item, index) => {
                item.choose = 0;
                if (item.choose == 1) {
                    chooseNum++;
                }
            });
            this.setData({
                chooseAll: 0,
                list: list,
                chooseNum: chooseNum
            });
        }
    }
});